package com.hc.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.hc.dao.IBaseDao;
import com.hc.util.GenericToModelUtil;

@Repository
public class BaseDaoImpl<T> implements IBaseDao<T> {
	@Resource
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	public Serializable save(T o) {
		return this.getCurrentSession().save(o);
	}

	public void delete(T o) {
		this.getCurrentSession().delete(o);
	}

	public void update(T o) {
		this.getCurrentSession().update(o);
	}

	public void saveOrUpdate(T o) {
		this.getCurrentSession().saveOrUpdate(o);
	}

	@SuppressWarnings("unchecked")
	public List<T> find(String hql) {
		getCurrentSession().clear();
		return this.getCurrentSession().createQuery(hql).list();
	}

	@SuppressWarnings("unchecked")
	public List<T> find(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		List<T> obj = q.list();
		getCurrentSession().clear();
		return obj;
	}

	@SuppressWarnings("unchecked")
	public List<T> find(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		getCurrentSession().clear();
		return q.list();
	}

	@SuppressWarnings("unchecked")
	public List<T> find(String hql, Object[] param, Integer page, Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		getCurrentSession().clear();
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}
	
	
	@SuppressWarnings("unchecked")
	public List<T> findByAuto(String hql, Object[] param, Integer page, Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		System.err.println(hql);
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				if ( param[i]==null){
					continue;
				}
				System.err.println(param[i]);
				q.setParameter(i, param[i]);
			}
		}
		getCurrentSession().clear();
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	@SuppressWarnings("unchecked")
	public List<T> find(String hql, List<Object> param, Integer page, Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	@SuppressWarnings("unchecked")
	public T get(Class<T> c, Serializable id) {
		return (T) this.getCurrentSession().get(c, id);
	}

	public T get(String hql, Object[] param) {
		List<T> l = this.find(hql, param);
		if (l != null && l.size() > 0) {
			return l.get(0);
		} else {
			return null;
		}
	}
	
	public T get(String hql, List<Object> param) {
		List<T> l = this.find(hql, param);
		if (l != null && l.size() > 0) {
			return l.get(0);
		} else {
			return null;
		}
	}

	public Long count(String hql) {
		return (Long) this.getCurrentSession().createQuery(hql).uniqueResult();
	}

	public Long count(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return (Long) q.uniqueResult();
	}

	public Long count(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return (Long) q.uniqueResult();
	}

	public Integer executeHql(String hql) {
		return this.getCurrentSession().createQuery(hql).executeUpdate();
	}

	public Integer executeHql(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.executeUpdate();
	}

	public Integer executeHql(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.executeUpdate();
	}
	@SuppressWarnings("unchecked")
	public List<T> executeLikeSql(Class<T> clazz, String sql,String type ,String param) throws Exception {
		Session session = this.getCurrentSession();
		Query q = session.createSQLQuery(sql);
		q.setString("type", type);
		q.setString("name", "%" + param + "%");
		ArrayList<Object> list = (ArrayList<Object>) q.list();
		GenericToModelUtil g = new GenericToModelUtil();
		List<T> t = new ArrayList<T>();
		for (Iterator<Object> it = list.iterator(); it.hasNext();) {
			Object[] ob = (Object[]) it.next();
			T instance = (T) clazz.newInstance();
			t.add(g.loadValue(clazz, instance, ob));
		}
		this.getCurrentSession().clear();
		return t;
	}
	@SuppressWarnings("unchecked")
	public List<T> executeSql(Class<T> clazz, String sql, Object[] param) throws Exception {
		Session session = this.getCurrentSession();
		Query q = session.createSQLQuery(sql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		ArrayList<Object> list = (ArrayList<Object>) q.list();
		GenericToModelUtil g = new GenericToModelUtil();
		List<T> t = new ArrayList<T>();
		for (Iterator<Object> it = list.iterator(); it.hasNext();) {
			Object[] ob = (Object[]) it.next();
			T instance = (T) clazz.newInstance();
			t.add(g.loadValue(clazz, instance, ob));
		}
		this.getCurrentSession().clear();
		return t;
	}
	/**
	 * 
	 * @Description： 条件查询
	 * @param clazz
	 * @param sql
	 * @param param
	 * @return
	 * @throws Exception
	 * @author:  
	 * @since: 2017年6月26日 上午11:47:08
	 */
	@SuppressWarnings("unchecked")
	public List<T> executeSqlAuto(Class<T> clazz, String sql, Object[] param) throws Exception {
		Session session = this.getCurrentSession();
		Query q = session.createSQLQuery(sql);
		int index = 0;
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				if (param[i]!=null){
					q.setParameter(index, param[i]);
					index = index+1;
				}
			}
		}
		ArrayList<Object> list = (ArrayList<Object>) q.list();
		GenericToModelUtil g = new GenericToModelUtil();
		List<T> t = new ArrayList<T>();
		for (Iterator<Object> it = list.iterator(); it.hasNext();) {
			Object[] ob = (Object[]) it.next();
			T instance = (T) clazz.newInstance();
			t.add(g.loadValue(clazz, instance, ob));
		}
		this.getCurrentSession().clear();
		return t;
	}
}
